; (function (w) {

  new Vue({
    el: '#businessMange',
    data: {
      quanxian: window.localStorage.getItem('permissions'),
      listMenu: [
        {
          name: '激活',
          num: 0,
          code: 'SJJH'
        },
        {
          name: '已关闭',
          num: 0,
          code: 'SJGB'
        },
        {
          name: '已转客户',
          num: 0,
          code: 'YZKH'
        }
      ],
      industryType: [],
      selectedIndustryType: [],
      selectedListMenuCode: [],
      policysList: [],
      rperson: {
        ids: [],
        name: null,
        list: []
      },       // 对接人
      totalPage: 0,
      totalCount: 0,
      currentPage: 1,
      pageCount: ['10'],
      pageCounts: [{ name: '10', code: '10' }, { name: '15', code: '15' }, { name: '20', code: '20' }],
      data: {},
      keyword: '',
      importFlag: false,
      dockpeople: [], //对接人
      dockVal: '',
      channelType: [], //渠道分类
      channerCode: [],
      channerName: [],  //渠道名称
      channerNameCode: [],
      source: [], //商机来源
      sourceCode: [],
      through: false,
      notThrough: false,
      detection: false, //检测
      detectionBtn: true,
      isShowDownload: false,
      is500: false,
      srcUrl: '',
      isExtenseFlag: false,
      columns: '',
      orders: '',
      configType: 'WZ', //默认完整WZ     JD简单，WZ完整，自定义配置PZ
      module: '#',
      headArr: [],
      sortTableTitle: [],
      isScroll: false,
      isShowTableLayer: false,
      dateTimeUpdate: [],
      isActiveCode: '#',
      tbTitleWidth: {},
      listObj: {},


      repeatSearchFlag: false,
      repeatXSlist: [],
      selectedXS: [],
      selectedCopyXS: [],
      repeatXSflag:false,

      loadingflag:true

    },
    filters: {
      indexAdd(i) {
        return Number(i) < 10 ? '0' + (i + 1) : i;
      },
      formatDate(time) {
        if (time === undefined) {
          return '--'
        }
        var date = new Date(time)
        return hlDateUtil.formatUTCDate(date, 'yyyy-MM-dd')
      },
      formatYYYYMMddHHmm(item) {
        if (!item) return '--'
        return hlDateUtil.formatUTCDate(item, 'yyyy-MM-dd HH:mm')
      },
      stateReturn(str) {
        var result;
        switch (str) {
          case 'SJJH':
            result = '激活'
            break;
          case 'SJGB':
            result = '已关闭'
            break;
          case 'YZKH':
            result = '已转客户'
            break;
          default:
            result = '--'
        }
        return result
      },
      formatUndefined(item) {
        if (!item) return '--'
        if (typeof item == 'number') {
          if (!isNaN(item) && item.toString().length == 13) {
            return hlDateUtil.formatUTCDate(item, 'yyyy-MM-dd HH:mm')
          };
        }
        return item
      },
    },
    methods: {
      repeatXSfun(){
        this.currentPage = 1;
        this.repeatXSflag = true
        this.getListData()
      },
      resetRepeatXS(){
        this.currentPage = 1;
        this.repeatXSflag = false
        this.getListData()
      },
      sureAddRepeatFun() {
        this.selectedCopyXS = JSON.parse(JSON.stringify(this.selectedXS))
        this.repeatSearchFlag = false
        hlhttp.put({
          url: 'biz_opportunity/repeat_rules',
          data: {
            rules: this.selectedXS.length ? this.selectedXS + '' : '#'
          },
          success: data => {
            this.getSelectedXSdict()
          }
        })
      },
      addRepeatSearchFun() {
        this.repeatSearchFlag = true
        this.selectedXS = JSON.parse(JSON.stringify(this.selectedCopyXS))
      },
      // 获取重复线索字典
      getBusinessDict() {
        hlhttp.get({
          url: 'configure/dict',
          // data: { 'module_code': 'ZHSH', 'config_name': 'ZS_REPEAT_RULE' },
          data: { 'module_code': 'ZHSH0SHJI', 'config_name': 'SJ_REPEAT_RULE' },
          success: data => {
            data.map(item => {
              item.name = item.dictName
              item.code = item.dictCode
            })
            this.repeatXSlist = data
          }
        })
      },
      // 获取已选线索
      getSelectedXSdict() {
        hlhttp.get({
          url: 'biz_opportunity/repeat_rules',
          success: data => {
            this.selectedCopyXS = data
          }
        })
      },
      selectedXSfun(code) {
        let index = this.selectedXS.indexOf(code)
        if (index == -1) {
          this.selectedXS.push(code)
        } else {
          this.selectedXS.splice(index, 1)
        }
      },
      selectMenuList(data) {
        this.getListData()
      },
      //获取线索来源
      setSourceCode(arr) {
        this.sourceCode = [arr[0].code]
      },
      resetTabbar() {
        this.selectedListMenuCode = [];
        this.getListData()
      },
      openSearch() {
        if (this.isExtenseFlag == true) {
          this.isExtenseFlag = false;
          return false;
        }
        if (this.isExtenseFlag == false) {
          this.isExtenseFlag = true;
          return false;
        }
      },
      //输入对接人进行搜索
      onInputDJR(e) {
        if (!this.rperson.name) {
          if (!e.data) {
            this.rperson.name = '';
            this.getDataGroup()
            return
          };
          this.rperson.name = e.data;
        }
        this.getDataGroup()
      },
      //判断有没有ids
      ifrpersonIds() {
        if (!this.rperson.ids.length) {
          this.rperson.name = null
        }
      },
      //设置对接人name，获取id
      setRperson: function (name, id) {
        this.rperson.ids = [];
        this.rperson.name = name;
        this.rperson.ids.push(id)
        this.rperson.list = [];
      },
      //获取租户的数据访问策略
      policys: function () {
        var _this = this;
        hlhttp.get({
          url: 'data_policys',
          success: function (res) {
            _this.policysList = res;
          }
        })
      },
      //搜索负责人工作组组员列表
      getDataGroup: function () {
        var _this = this;
        _this.rperson.ids = [];
        hlhttp.get({
          url: 'tenants/users/_search',
          data: {
            keyword: _this.rperson.name || '#',
            first_order: 'Y'
          },
          success: function (res) {
            _this.rperson.list = res;
            for (var i = 0; i < _this.rperson.list.length; i++) {
              if (_this.rperson.name.match(res[i])) {
                _this.rperson.ids.push(res[i].id)
              }
            }
          }
        })
      },
      //新建
      createSj() {
        hlUrlUtil.goTo('businessCreate', { page: 'businessMange', ddtab: true }, true)
      },
      goTodetail(id) {
        hlUrlUtil.goTo('businessDetails', { 'businessId': id, 'page': 'businessMange', 'ddtab': true }, true)
      },
      //导入
      importExcl() {
        this.importFlag = true;
        this.is500 = false
      },
      //关闭
      importExclClose() {
        this.importFlag = false;
        this.detection = false;
        this.notThrough = false;
        this.detectionBtn = true;
        this.is500 = false;
        this.through = false;
        this.srcUrl = '';
        $('.filename-i').val('')
      },
      //渠道分类
      getSJCHANNELCATE() {
        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH', config_name: 'ZS_CHANNEL_CATE' },
          success: res => {
            this.channelType = hlArrayUtil.replaceData(res, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }]);
          }
        })
      },
      //渠道名称
      getSJCHANNELNAME() {
        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH', config_name: 'ZS_CHANNEL_NAME', parent_dict_code: this.channerCode[0] },
          success: res => {
            this.channerName = hlArrayUtil.replaceData(res, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }]);
          }
        })
      },
      //set渠道分类
      setSJCHANNELCATE(arr) {
        this.channerCode = [arr[0].code];
        this.getSJCHANNELNAME()
      },
      //商机来源
      getSJsource() {
        hlhttp.get({
          url: 'configure/dict',
          data: { module_code: 'ZHSH0SHJI', config_name: 'SJ_SOURCE' },
          success: res => {
            this.source = hlArrayUtil.replaceData(res, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }]);
          }
        })
      },
      selectChannelName(arr) {
        this.channerNameCode = [arr[0].code];
      },
      //获取data
      getListData() {
        const self = this
        this.loadingflag = true
        var data = {
          "origin": self.sourceCode.length ? self.sourceCode[0] : '#',
          "assignee_id": self.rperson.ids.toString() || '#',
          "channel_category_code": self.channerCode.length ? self.channerCode[0] : '#',
          "channel_name_code": self.channerNameCode.length ? self.channerNameCode[0] : '#',
          "status": self.selectedListMenuCode.length ? self.selectedListMenuCode.toString() : '#',
          'industry': self.selectedIndustryType.length ? self.selectedIndustryType + '' : '#',
          "keyword": self.keyword || '#',
          "limit": self.pageCount[0],
          "page": self.currentPage,
          "columns": self.columns || '#',
          "orders": self.orders || '#'
        }
        if (this.repeatXSflag) {
          data.repeat_flag = 'Y'
          data.repeat_rules = this.selectedCopyXS + ''
        }

        hlhttp.get({
          url: 'customer/biz_oppt_search',
          data: data,
          success: res => {
            self.totalCount = res.totalCount
            self.totalPage = res.totalPage
            self.listObj = res;
            self.listMenu[0].num = res.countInfo.SJJH;
            self.listMenu[1].num = res.countInfo.SJGB;
            self.listMenu[2].num = res.countInfo.YZKH;
            self.loadingflag = false
          }
        })
      },
      setMoreNum(arr) {
        this.pageCount = [arr[0].code];
        this.getListData()
      },
      //重置
      resetData() {
        this.sourceCode = [];
        this.rperson.ids = '';
        this.rperson.name = '';
        this.selectedIndustryType = []
        this.channerCode = [];
        this.channerNameCode = [];
        this.keyword = '';
        this.currentPage = 1;
        this.orders = '';
        this.isActiveCode = '';
        this.columns = '';

        this.getListData()
      },
      //查询
      search() {
        this.currentPage = 1;
        this.pageCount = ['10'];
        this.getListData();
      },
      //上床模板
      downloadxslc() {
        $('.upload-filess').trigger('click')
        this.detection = false;
        this.notThrough = false;
        this.detectionBtn = true;
        this.is500 = false;
        this.through = false;
        this.srcUrl = '';
        $('.filename-i').val('')
      },
      detectionXslx() {
        this.detection = true;
        this.detectionBtn = false;
        if (!this.srcUrl) return;

        hlhttp.put({
          baseUrl: exclUrl,
          url: 'resource/opportunity_check',
          data: { file_name: this.srcUrl },
          success: res => {
            if (res.code == 200) {
              this.detection = false;
              this.notThrough = false;
              this.detectionBtn = false;
              this.through = true;
              this.isShowDownload = true;
              this.is500 = false;
            }
            if (res.code == 400) {
              this.detection = false;
              this.notThrough = true;
              this.detectionBtn = false;
              this.is500 = false;
              this.through = false;
              this.isShowDownload = true;
              this.srcUrl = res.url
            }
            if (res.code == 500) {
              this.is500 = true;
              this.detection = false;
              this.notThrough = false;
              this.detectionBtn = false;
              this.through = true;
              this.srcUrl = res.url;
              this.isShowDownload = true;
            }
          },
          error: res => {
            hl.toast('请使用标准模板上传！')
            this.detection = false;
            this.notThrough = true;
            this.detectionBtn = false;
            this.isShowDownload = false;
            this.is500 = false;
            this.through = false;
          }
        })
      },
      //submit xslc
      submibXslc() {
        hlhttp.post({
          baseUrl: exclUrl,
          url: 'resource/opportunity_upload_data',
          data: { file_name: this.srcUrl.substr(this.srcUrl.lastIndexOf('/') + 1) },
          success: () => {
            hl.toast('上传成功')
            this.getListData()
            this.importExclClose()
          }
        })
      },

      //更新时间
      sortBySTTime(item) {
        this.isActiveCode = item.value;
        this.orders = item.orders
        this.columns = item.columns
        this.getListData()
      },
      //获取自定义表头配置状态
      configTypeDt() {
        hlhttp.get({
          url: 'configure/customize_column_header/type',
          data: { module: 'ZHSH_XSGL' },
          success: (data) => {
            this.configType = data.configType;
            this.customizeHeaderDt()
          }
        })
      },
      //获取自定义表头配置信息
      customizeHeaderDt() {
        hlhttp.get({
          url: 'configure/customize_column_header',
          data: { module: 'ZHSH_XSGL' },
          success: (data) => {
            data.customConfigList.map(item => {
              item.seq = Number(item.seq)
              item.customSeq = Number(item.customSeq)
              item.isActive = 'N'
              if (item.orderFlag == 'Y') {
                this.dateTimeUpdate.push(item.value)
              }
            })
            if (this.configType == 'JD' || this.configType == 'WZ') {
              data.customConfigList = data.customConfigList.sort(hlArrayUtil.compareAsc('seq'))
            }
            if (this.configType == 'PZ') {
              data.customConfigList = data.customConfigList.sort(hlArrayUtil.compareAsc('customSeq'))
            }
            this.headArr = JSON.parse(JSON.stringify(data.customConfigList));
            this.sortTableTitle = JSON.parse(JSON.stringify(data.customConfigList));
            this.tbTitleWidthCalculation()
          }
        })
      },
      //宽度计算
      tbTitleWidthCalculation() {
        let w = 0, flag = false;
        this.headArr.map(item => {
          if (item.value == '序') {
            flag = true;
          }
        })
        w = this.headArr.length * 120 - 80;
        this.tbTitleWidth = { width: w + 'px' }
      },
      //弹窗显示
      showTableLayer() {
        this.isShowTableLayer = true;
      },
      //关闭自定义表头弹窗
      closeTableLayer() {
        this.isShowTableLayer = false;
      },
      //简单完整配置切换
      selectTableType(obj) {
        this.configType = obj.code;
        this.updateConfigType()
      },
      //更新自定义表头配置状态
      updateConfigType() {
        hlhttp.put({
          url: 'configure/customize_column_header/type',
          data: { module: 'ZHSH_XSGL', config_type: this.configType },
          success: () => {
            this.configTypeDt()
          }
        })
      },
      // 确定提交自定义表格
      confirmSubmitTable(data) {
        let dataArr = [];
        data.map((item, index) => {
          dataArr.push({
            "base_flag": item.baseFlag,
            "seq": item.seq,
            "custom_show_flag": item.customShowFlag,
            "custom_seq": item.newIndex,
            "custom_display_name": item.customDisplayName,
            "union_table_part": item.unionTablePart,
            "dict_code": item.dictCode,
            "config_type": item.configType,
            "name": item.name,
            "value": item.value,
            "style_width": item.styleWidth,
            "flex": item.flex,
            "order_flag": item.orderFlag,
            "order_name": item.orderName
          })
        })
        hlhttp.put({
          url: 'configure/customize_column_header',
          data: {
            module: 'ZHSH_XSGL',
            custom_config: JSON.stringify(dataArr)
          },
          success: (data) => {
            hl.toast('操作成功')
            this.closeTableLayer()
            this.updateConfigType()
          }
        })
      },
      // 获取产业
      getIndustryType() {
        hlhttp.get({
          url: 'configure/industry/dict',
          success: data => {
            data.unshift({
              'dictName': '全部',
              'dictCode': '#'
            })
            this.industryType = hlArrayUtil.replaceData(data, [{ key: 'dictName', rKey: 'name' }, { key: 'dictCode', rKey: 'code' }]);
          }
        })
      },
    },
    created() {
      loginPrem.loginList()
    },
    mounted: function () {
      hlSelectUi()
      this.getSJsource()
      this.policys()
      this.getSJCHANNELCATE()
      this.getListData()
      this.configTypeDt()
      this.getIndustryType()

      this.getBusinessDict()
      this.getSelectedXSdict()

      var _this = this;
      document.body.onclick = function () {
        _this.rperson.list = [];
      }
      fileUpload('.upload-filess', uploadUrl, (src) => {
        if (src.json == undefined) return;
        if (!(src.json.name.indexOf('xls') > -1)) {
          hl.toast('请上传正确的模板')
          return
        }
        if (!(src.json.name.indexOf('xlsx') > -1)) {
          hl.toast('请上传正确的模板')
          return
        }
        this.srcUrl = src.json.url.substr(src.json.url.lastIndexOf('/') + 1);
        $('.filename-i').val(src.json.name)
      }, function () {

      })
    }
  })

}(window))
